package cn.zz.veggie.persistence.repository;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.collection.CollectionUtil;
import cn.zz.comm.core.types.query.PageQuery;
import cn.zz.comm.exception.classes.FatalErrorException;
import cn.zz.comm.util.CollUtil;
import cn.zz.comm.util.CollectorUtil;
import cn.zz.comm.util.NumberUtil;
import cn.zz.veggie.model.domain.Purchase;
import cn.zz.veggie.model.query.PurchaseQuery;
import cn.zz.veggie.persistence.dao.PurchaseMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 采购统计表(Purchase)repo
 *
 * @author zhongq
 * @since 2024-06-03 23:40:02
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class PurchaseRepository {

	private final PurchaseMapper purchaseMapper;

	public Purchase findById(Integer id) {
		if (NumberUtil.isPositive(id)) {
			return purchaseMapper.selectById(id);
		}
		return null;
	}

	public Map<Integer, Purchase> findByIds(Collection<Integer> ids) {
		if (CollectionUtil.isEmpty(ids)) {
			return Collections.emptyMap();
		}
		final List<Purchase> records = purchaseMapper.selectBatchIds(ids);
		return CollectorUtil.btmByKeyFun(records, Purchase::getId);
	}

	public Purchase findOne(PurchaseQuery query) {
		final LambdaQueryWrapper<Purchase> queryWrapper = query.toQueryWrapper();
		final Page<Purchase> page = query.toPage(PageQuery.first().setSearchCount(false));
		final List<Purchase> records = purchaseMapper.selectList(page, queryWrapper);
		return CollUtil.getFirst(records);
	}

	public List<Purchase> findByQuery(PurchaseQuery query, PageQuery page) {
		final LambdaQueryWrapper<Purchase> queryWrapper = query.toQueryWrapper();
		final Page<Purchase> queryPage = query.toPage(page);
		List<Purchase> records = purchaseMapper.selectList(queryPage, queryWrapper);
		page.setTotal((int) queryPage.getTotal());
		return records;
	}

	public void save(Purchase purchase) {
		Objects.requireNonNull(purchase);
		if (purchase.getId() == null) {
			// insert
			final int effect = purchaseMapper.insert(purchase);
			if (effect != 1) {
				throw new FatalErrorException("created purchase fail ");
			}
			log.info("created purchase for {}", purchase.getId());
		} else {
			// update
			final int effect = purchaseMapper.updateById(purchase);
			if (effect != 1) {
				throw new FatalErrorException("update purchase fail ");
			}
			log.info("updated purchase for {}", purchase.getId());
		}
	}

	public void deleteById(Integer id) {
		Objects.requireNonNull(id);
		final int effect = purchaseMapper.deleteById(id);
		if (effect != 1) {
			log.error("delete");
			throw new FatalErrorException("delete purchase fail ");
		}
		log.info("delete purchase for {}", id);
	}

	public void deleteByIds(Collection<Integer> ids) {
		if (CollUtil.isEmpty(ids)) {
			return;
		}
		final int effect = purchaseMapper.deleteByIds(ids);
		log.info("delete purchase for {}", effect);
	}
}